
export const downloadFile = (url: string, name: string): void => {
  const a = document.createElement('a');
  const body = document.querySelector('body');
  a.download = name;
  a.href = url;
  body!.append(a);
  a.click();
  window.URL.revokeObjectURL(a.href);
  a.remove();
};
//动态表单判断是否隐藏
export const isHidden = (data: any, obj: any) => {
  let state = true;
  if( obj.hidden ){
    return false
  }
  if (Array.isArray(obj.showKey) && obj.showKey.length) {
    //优化下
    state = obj.showKey.every((item: any) => {
      let isBooles = true; //默认显示
      if (item.invert) {
        if (Array.isArray(item.value)) {
          if (item.parent) {
            if (item.value.indexOf(data[item.parent][item.key]) !== -1) {
              isBooles = false;
            }
          } else {
            if (item.value.indexOf(data[item.key]) !== -1) {
              isBooles = false;
            }
          }
        } else if(typeof item.value !== 'object' && typeof data[item.key]!== 'object') {
          if (item.parent) {
            if (data[item.parent][item.key] === item.value) {
              isBooles = false;
            }
          } else {
            if (data[item.key] === item.value) {
              isBooles = false;
            }
          }
        }else {
          isBooles = objectsEqual(data[item.key], item.value);
        }
      } else {
        if (Array.isArray(item.value)) {
          if (item.parent) {
            if (item.value.indexOf(data[item.parent][item.key]) == -1) {
              isBooles = false;
            }
          } else {
            if (item.value.indexOf(data[item.key]) == -1) {
              isBooles = false;
            }
          }
        } else if(typeof item.value !== 'object' && typeof data[item.key]!== 'object'){ 
          if (item.parent) {
            if (data[item.parent][item.key] !== item.value) {
              isBooles = false;
            }
          } else {
            if (data[item.key] !== item.value) {
              isBooles = false;
            }
          }
        } else {
          isBooles = objectsEqual(data[item.key], item.value);
        }
      }
      return isBooles;
    });
  }
  if( obj.localVerify ){
    state = obj.localVerify(data);
  }
  return state;
};
//动态改变pinia数据
export const getMenNum = (key: any, option: any, value: any) => {
  return Object.assign(
    {},
    {
      key,
      option,
      value
    }
  );
};

function objectsEqual(obj1, obj2) {
    // 如果两个对象引用相同，则它们相等
    if (obj1 === obj2) {
      return true;
    }
  
    // 如果两个对象不是对象类型或其中一个是 null，则它们不相等
    if (typeof obj1 !== 'object' || typeof obj2 !== 'object' || obj1 === null || obj2 === null) {
      return false;
    }
  
    // 获取两个对象的属性名称
    const obj1Props = Object.getOwnPropertyNames(obj1);
    const obj2Props = Object.getOwnPropertyNames(obj2);
  
    // 如果两个对象的属性数量不同，则它们不相等
    if (obj1Props.length !== obj2Props.length) {
      return false;
    }
  
    // 遍历第一个对象的属性并比较它们的值
    for (let i = 0; i < obj1Props.length; i++) {
      const propName = obj1Props[i];
  
      // 如果第二个对象不包含相同的属性，则它们不相等
      if (!obj2.hasOwnProperty(propName)) {
        return false;
      }
  
      // 如果属性值是对象，则递归比较它们
      if (typeof obj1[propName] === 'object' && typeof obj2[propName] === 'object') {
        if (!objectsEqual(obj1[propName], obj2[propName])) {
          return false;
        }
      } else if (obj1[propName] !== obj2[propName]) {
        // 如果属性值不是对象，则比较它们的值
        return false;
      }
    }
  
    // 如果所有属性和属性值都相同，则这两个对象相等
    return true;
  }


//判断必填字段array:数据源，fields需要判断的字段['a','b']
export const checkFields = (array, fields) => {
  return array.every(obj => fields.every(field => obj[field]));
}

//删除指定数组里面的匹配id项
export const removeIdFromArray = (arr, ids) => {
  for (let i = 0; i < ids.length; i++) {
    let idToDelete = ids[i];
    for (let j = 0; j < arr.length; j++) {
      if (arr[j].id === idToDelete) {
        arr.splice(j, 1);
        j--;
      }
    }
  }
}

// 判断是否有值
export function isDefined(val) {
  return !(val === null || val === undefined || val === '');
}
//树形数据找到对应匹配id的对应对象
export const findObjectByIdInArray = (treeArray, targetId, id='value', children ='children') => {
  for (const node of treeArray) {
    if (node[id] === targetId) {
      return node;
    }
    let result = null;
    if(node[children]){
      result = findObjectByIdInArray(node[children], targetId, id, children);
    }
    if (result) {
      return result;
    }
  }
  return null;
}
